package com.cplatform.movie.back.sale.service;

import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.cplatform.dbhelp.DbHelper;
import com.cplatform.dbhelp.page.Page;
import com.cplatform.movie.back.model.SessionUser;
import com.cplatform.movie.back.sale.dao.ItemSaleDao;
import com.cplatform.movie.back.sale.entity.ItemSale;

/**
 * Title. 商品业务层 <br>
 * Description.
 * <p>
 * Author: liuyt
 * <p>
 * Version: 1.0
 * <p>
 */
@Service
public class ItemSaleService {
	@Autowired
	private ItemSaleDao itemSaleDao;
	@Autowired
	private ImageDetailService imageDetailService;
	@Autowired
	private DbHelper dbHelper;

	/**
	 * @Title: 商品添加 <br>
	 * @param itemSale
	 * @param jzImgArray
	 * @throws Exception
	 */
	@Transactional
	public ItemSale save(ItemSale itemSale, String[] jzImgArray, String[] jzImgOrderArray, String[] jzImgIdArray) throws Exception {
		itemSale = itemSaleDao.save(itemSale);
		if (jzImgArray != null && jzImgArray.length > 0) {
			imageDetailService.saveImageDetails(itemSale, jzImgArray, jzImgOrderArray, jzImgIdArray);
		}
		return itemSale;
	}

	/**
	 * @Title: 查询商品分页数据 <br>
	 * @param itemSale
	 * @param page
	 * @return
	 * @throws SQLException
	 */
	public Page<ItemSale> list(ItemSale itemSale, int page, SessionUser sessionUser) throws SQLException {
		StringBuilder sb = new StringBuilder("select t.*, type.name as typeName from T_SMALL_SHOP_ITEMS t, T_SYS_TYPE type where t.IS_VALID=1 and t.TYPE_ID=type.ID(+) ");
		List<Object> paramList = new ArrayList<Object>();

		// -- 超级管理员可以查看任意商户商品
		if (sessionUser.getSysUser().getId() != 0) {
			sb.append(" and t.store_id=(select store.id from t_store store where store.sys_user_id=?)");
			paramList.add(sessionUser.getSysUser().getId());
		}

		if (itemSale.getId() != null) {
			sb.append(" and t.id=?");
			paramList.add(itemSale.getId());
		}

		if (itemSale.getPostFlag() != null) {
			sb.append(" and t.post_flag=?");
			paramList.add(itemSale.getPostFlag());
		}

		if (itemSale.getStatus() != null) {
			sb.append(" and t.status=?");
			paramList.add(itemSale.getStatus());
		}

		if (itemSale.getSaleStatus() != null) {
			sb.append(" and t.sale_status=?");
			paramList.add(itemSale.getSaleStatus());
		}

		if (StringUtils.isNotBlank(itemSale.getCreateTime())) {
			sb.append(" and t.create_time>?");
			paramList.add(itemSale.getCreateTime() + "000000");
		}

		if (StringUtils.isNotBlank(itemSale.getCreateTimeEnd())) {
			sb.append(" and t.create_time<?");
			paramList.add(itemSale.getCreateTimeEnd() + "235959");
		}

		if (StringUtils.isNotBlank(itemSale.getSaleStartTime())) {
			sb.append(" and t.sale_start_time>?");
			paramList.add(itemSale.getSaleStartTime() + "000000");
		}

		if (StringUtils.isNotBlank(itemSale.getSaleStopTime())) {
			sb.append(" and t.sale_start_time<?");
			paramList.add(itemSale.getSaleStopTime() + "235959");
		}

		if (StringUtils.isNotBlank(itemSale.getName())) {
			sb.append(" and t.name like ?");
			paramList.add("%" + itemSale.getName() + "%");
		}

		sb.append(" order by t.id desc");

		return dbHelper.getPage(sb.toString(), ItemSale.class, page, Page.DEFAULT_PAGESIZE, paramList.toArray());
	}

	/**
	 * 根据商品ID查询
	 * 
	 * @throws SQLException
	 **/
	public ItemSale findById(Long id) throws SQLException {
		String sql = "select t.*, type.name as typeName from T_SMALL_SHOP_ITEMS t, T_SYS_TYPE type where t.id=? and t.IS_VALID=1 and t.TYPE_ID=type.ID(+)";
		return dbHelper.getBean(sql, ItemSale.class, new Object[] { id });
	}

	/**
	 * 新增/更新单个商品
	 * 
	 * @return
	 **/
	public ItemSale saveOne(ItemSale itemSale) {
		return itemSaleDao.save(itemSale);
	}

	/** 商品删除 **/
	public void delete(Long id) {
		ItemSale itemSale = itemSaleDao.findOne(id);
		itemSale.setIsValid(0l);
		itemSaleDao.save(itemSale);
	}

	/**
	 * @Title: 商品关联活动 <br>
	 * @param acId
	 * @param id
	 * @throws
	 */
	@Transactional
	public void linkActivities(Long acId, Long id) throws Exception {
		// -- 清除旧数据
		String sql = "delete from t_small_item_activities_link t where t.T_SMALL_ITEMS_ID=?";
		dbHelper.execute(sql, new Object[] { id });
		if (acId != null) {
			sql = "insert into t_small_item_activities_link values(?,?)";
			dbHelper.execute(sql, new Object[] { id, acId });
		}
	}

	/**
	 * @throws SQLException
	 * @Title: 获取商品关联活动数组 0:活动名称  1:活动id<br>
	 * @param id
	 * @return
	 * @throws
	 */
	public String[] findLinkAcById(Long id) throws SQLException {
		String sql = "select ac.name, ac.id from t_small_activieits ac, t_small_item_activities_link link where"
				+ " link.t_small_items_id=? and ac.id=link.t_small_activities_id(+)";
		return dbHelper.getArray(sql, new Object[] { id });
	}

}
